package cn.jbit.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 源码学院只为培养优秀的bat程序员而生
 *
 * @author 学员张在成
 * @data 2020/8/2
 */
public class ServerBioSingle {

    public static void main(String[] args) throws IOException {
        //建立个socket
        ServerSocket serverSocket=null;
        System.out.println("服务器启动成功");
        InputStream inputStream=null;
        OutputStream outputStream=null;
        Socket socket=null;
        serverSocket=new ServerSocket(8080);
        //与客户端建立连接,进行自旋,建立多个请求
        while (true){
            socket=serverSocket.accept();  //进行3次握手
            System.out.println("新连接："+socket.toString());
            //接收数据
            inputStream=socket.getInputStream();
            byte[] buffer=new byte[1024];
           // BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            //进行打印
            int length=0;
            while ((length=inputStream.read(buffer))>0){ //阻塞
                System.out.println("input is:"+new String(buffer));
                outputStream=socket.getOutputStream();
                outputStream.write("sucesss".getBytes());
            }
            //System.out.println("接收到数据："+reader.readLine());
        }
    }
}
